
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>添加管理员-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <link rel="stylesheet" href="../../static/css/font.css" />
    <link rel="stylesheet" href="../../static/css/weadmin.css" />
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="weadmin-body">
    <form class="layui-form" >
        <div class="layui-form-item" id="tree">

        </div>
        <div class="layui-btn-container">
            <button type="button" class="layui-btn " lay-demo="getChecked">立即提交</button>
        </div>
    </form>
</div>
<script src="../../static/layui/layui.js" charset="utf-8"></script>
<script>
    layui.extend({
        admin: '{/}../../static/js/admin'
    });
    layui.use(['form','tree','util'], function(){
        var tree = layui.tree;
        var util = layui.util;
        var $ = layui.jquery ;
        var form = layui.form,formdata=JSON.stringify(parent._formdata);
        var all=parent._formdata["all"],exist=parent._formdata["exist"];
        var data=getData(all);
        tree.render({
            elem: '#tree'
            ,data: data
            ,showCheckbox: true  //是否显示复选框
            ,id: 'treeId1'
        });
        var existIds =new Array();
        for (var i = 0; i < exist.length; i++) {
            existIds.push(exist[i].id);
        }
        tree.setChecked('treeId1', existIds);
        function getData(all){
            var data={},children1={},datalist= new Array();
            for (var i = 0; i < all.length; i++) {
                if(all[i].children != null && all[i].children.length>0){
                    children1 = getData(all[i].children);
                }
                data={title:all[i].name,id:all[i].id,
                children:children1,spread:true}
                datalist.push(data);
            }
            return datalist;
        }
        //按钮事件
        util.event('lay-demo', {
            getChecked: function(){
                var checkedData = tree.getChecked('treeId1'); //获取选中节点的数据
                console.log(checkedData);
                var ids = getIds(checkedData);
                // layer.alert(JSON.stringify(checkedData), {shade:0});
                ids=ids.substr(1,ids.length-1);
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: "/rolePermission/"+parent._formdata['roleId']+"",
                    contentType: "application/json;charset=UTF-8",//指定消息请求类型
                    data: JSON.stringify({"strIds":ids}),
                    success: function (data) {
                        if(data.code == null || data.code != 200){
                            layer.msg(data.msg);
                        }else{
                            layer.alert("修改成功", {
                                icon: 6
                            },function () {
                                // 获得frame索引
                                var index = parent.layer.getFrameIndex(window.name);
                                //关闭当前frame
                                parent.layer.close(index);
                                //关闭父级页面的表格
                                parent.layui.table.reload('list');
                            });
                        }
                    }
                });
            }
        });
        function getIds(data) {
            var ids='';
            for (var i = 0; i < data.length; i++) {
                if (data[i].children!=null &&data[i].children.length>0){
                    ids+= getIds(data[i].children);
                }
                ids+=','+data[i].id;
            }
            return ids;
        }
    });
</script>
</body>
</html>